﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.CustomerSVC;
using System.ComponentModel;

namespace SMS.Controls
{
    public partial class SMSCustomer : UserControl
    {
        private ServiceProxy<CustomerMasterServiceClient> proxy = null;
        public SMSCustomer()
        {
            InitializeComponent();
            if (!DesignerProperties.IsInDesignTool)
            {
                proxy = new ServiceProxy<CustomerMasterServiceClient>("Service/CustomerMasterService.svc");
                //proxy.Client.SearchProductCompleted += new EventHandler<SearchProductCompletedEventArgs>(Client_SearchProductCompleted);
                proxy.Client.SearchCustomerCompleted += new EventHandler<SearchCustomerCompletedEventArgs>(Client_SearchCustomerCompleted);
                smsAutoComplete.SelectionChanged += new Action<object, object>(smsAutoComplete_SelectionChanged);
                smsAutoComplete.SearchTextChanged += new Action<string>(smsAutoComplete_SearchTextChanged);
                smsAutoComplete.TextChanged += new Action<string>(smsAutoComplete_TextChanged);
                smsAutoComplete.SetFilter(SearchCustomer);
                smsAutoComplete.SetDataTemplate(this.Resources["CustomerTemplate"] as DataTemplate);
                smsAutoComplete.SetValuePath("Customer_Name");
            }
        }

        void smsAutoComplete_TextChanged(string obj)
        {
            SearchText = obj;
        }


        private bool IsNewSearchTextRequired(string searchText)
        {
            return currentSearchText.ToLower() != searchText.ToLower();
        }
        private string currentSearchText = string.Empty;

        public bool SearchCustomer(string search, object value)
        {
            return (value as SMS.Entities.Customer).Customer_ID.ToString().Contains(search.ToLower()) ||
                (value as SMS.Entities.Customer).Customer_Name.ToLower().Contains(search.ToLower()) ||
                (value as SMS.Entities.Customer).Customer_Mobile.ToLower().Contains(search.ToLower());
            //return true;
        }

        void smsAutoComplete_SearchTextChanged(string obj)
        {

            if (IsNewSearchTextRequired(obj))
            {
                currentSearchText = smsAutoComplete.autoCompleteBox1.Text;
                VisualStateManager.GoToState(smsAutoComplete, "BusyState", true);
                proxy.Client.SearchCustomerAsync(string.Empty, obj);
            }
            SearchText = obj;

        }

        void smsAutoComplete_SelectionChanged(object arg1, object arg2)
        {
            SelectedCustomer = arg2 as SMS.Entities.Customer;
        }

        void Client_SearchCustomerCompleted(object sender, SearchCustomerCompletedEventArgs e)
        {
            smsAutoComplete.ItemSource = e.Result as IEnumerable<SMS.Entities.BaseClasses.BaseEntity>;
            VisualStateManager.GoToState(smsAutoComplete, "Normal", true);
        }





        public Entities.Customer SelectedCustomer
        {
            get { return (Entities.Customer)GetValue(SelectedCustomerProperty); }
            set
            {
                SetValue(SelectedCustomerProperty, value);
                smsAutoComplete.SetSelectedItem(value);
            }
        }

        // Using a DependencyProperty as the backing store for SelectedProduct.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedCustomerProperty =
            DependencyProperty.Register("SelectedCustomer", typeof(Entities.Customer), typeof(SMSCustomer), new PropertyMetadata(null, new PropertyChangedCallback(SelectedCustomerChanged)));

        public static void SelectedCustomerChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSCustomer;
            //if (control.SelectedProduct != e.NewValue as SMS.Entities.Product)
            //{
            control.SelectedCustomer = e.NewValue as SMS.Entities.Customer;
            //}
        }



        public string SearchText
        {
            get { return (string)GetValue(SearchTextProperty); }
            set { SetValue(SearchTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SearchText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SearchTextProperty =
            DependencyProperty.Register("SearchText", typeof(string), typeof(SMSCustomer), new PropertyMetadata(string.Empty, new PropertyChangedCallback(SearchTextPropertyChanged)));

        private static void SearchTextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSCustomer;
            control.SearchText = e.NewValue as string;
        }

    }
}
